package class202205.demo10.demo07;

import javax.swing.plaf.synth.SynthOptionPaneUI;

//写一个泛型类，类中有个方法，求数组最大值
/*class Alg<T extends Comparable<T>>{//泛型上界 使用extends
    //<T>擦除为Object  而Object没有实现ComperTo  所以.c点不出来 需要给T（Object）继承ComperTo
    public T findMax(T[] array){
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max.compareTo(array[i])<0){
                max = array[i];
            }
        }
        return max;
    }
}*/
class Alg2{
    /**
     *   静态泛型方法
     * 因为每次调用上面的findMax的时候，还必须new一个对象，来通过对象的引用调用findMax
     * 现在我想直接调用findMax  可不可以不实例化对象
     * 可以--->把上面方法写成静态的
     *随时调用，就得给static后面加<T>.
     * 然后把指定边界拷贝一下
     */
    public static <T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 0; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }
}
class Test<E extends Number>{

}
public class TestDemo2 {

    public static void main(String[] args) {
        /**
         * 好处就是不用实例化泛型对象了，因为上面是静态的泛型方法
         */
        Integer[] array ={1,4,5,21,8,19};
        Integer max = Alg2.findMax(array);
        System.out.println(max);
    }

    public static void main2(String[] args) {
        //泛型的上界
        Test<Integer> test = new Test<>();
        Test<Number> test1 = new Test<>();
        Test<Float> test2 = new Test<>();
        Test<Double> test3 = new Test<>();
}

    public static void main1(String[] args) {
       //引用类型:不能直接用大于小于号比较
       /* Alg<Integer> alg = new Alg<>();
        Integer[] array ={1,4,5,21,8,19};
        Integer max = alg.findMax(array);
        System.out.println(max);


        Alg<Student> alg1 = new Alg<>();
        Student[] students = new Student[3];
        students[0] = new Student("bir",15);
        students[1] = new Student("hello",25);
        students[2] = new Student("gaobo",5);

        //最大的学生怎么评判？
        Student student = alg1.findMax(students);
        System.out.println(student);*/
    }
}
